The U.S. trade deficit in July was nearly double June's the government said yesterday. The increase ended two months of improvement and sent the dollar plunging again on foreign exchange markets.

The Commerce Department said imports exceeded exports last month by $299 billion, up from $1.6 billion the previous month. The July deficit was the fourth largest on record and the worst since February.

The deterioration surprised top Carter administration officials, who had been predicting the improvement of May and June would continue through the remainder of the year. It also blunted the past week's comeback of the dollar.

Within minutes after the trade figures were announced, the dollar fell sharply against other major currencies. At the end of the day, the dollar was down 1.4 percent against the West German mark and 1.6 percent against the Japanese yen.

Charles L. Schultze, President Carter's chief economist, said while the administration was "disappointed" with the new figures, policymakers still expect the trade deficit to improve significantly in coming months.

The surfeit of imports over exports has been a major reason for the decline in the value of the dollar in recent months. The excess has left a glut of dollars overseas, prompting traders to unload U.S. currency.

Ironically, the decline in the dollar is expected to help reduce the U.S. trade deficit, as imported goods become more expensive here and U.S. exports grow more attractisve abroad. But anaylsts say the full impact is months away. Meanwhile, its main effect is simply to add to domestic inflation.

The July increase in the trade deficit came despite another drop in petroleum imports, which in previous months have been the deficit's leading cause. Oil imports fell 4.5 percent, after dropping 1 percent in June.

However, other imports increased significantly, in a variety of categories ranging from steel and from foreign-made automobiles to machinery, food and livestock, television receivers and telephone equipment. Meanwhile, exports declined.

The increase in steel imports was especially perplexing, since the administration recently has imposed a new "trigger price" system aimed at cutting back competition from foreign steelmakers.

Some government and private analysts contend the lates changes in U.S. and Japanese exchange rates has made the "trigger price" system less effective. The Treasury is considering revamping it. (Article on Page B1.)

There was substantial debate over the meaning of the July trade figures. Most analysts regarded them simply as an aberration, in effect making up for what may have been an exaggerated improvement in June.

However, both government and private analysts cautioned that they wanted to wait for the August trade statistics before saying for certain whether July was a fluke.

From a high of $4.52 billion in February, the trade deficit has shrunk eratically. The red-ink figure dropped to $2.24 billion in May and $1.59 billion in June, its lowest in 13 months.

Yesterday's figures showed imports up a sharp 7.7 percent, or $1.06 billion, to a new annual rate of $14.8 billion. By contrast, exports fell 2.7 percent, or $333.2 million, to $11.379 billion - their first drop in five months.

The decline in exports came in six of 10 major categories, including agricultural products and key manufactured goods. At the same time, the nation also sold less paper and paperboard and electrical machinery.

The combination of developments brought the trade deficit for the first seven months of 1978 to $19.36 billion - implying a figure for the year well in excess of last year's $26.6 billion.

Carter administration officials have been predicting the trade deficit for all of 1978 would come out "just slightly above" last year's total. They declined to revise their forecast in light of yesterday's report.

The trade deficit had been improving steadily since its peak this past February. Some analysts have speculated that by the end of this year, it should be running at an annual rate of about $18 billion.